
public class Play implements Runnable {
	public static int metro = 0; // time in ticks
	public static int bpm = 120; // --> duration (ms) from ticks and bpm (ticks/480)/(bpm/60000)
	public static int lastPch = 0;
	public static int lastInt = 0;
	public static int lastDur = 0;
	public static int pch = 0;
	public static int interval = 0;
	public static int dur = 0;
//	public static int vel = 100;
	public static int count = 0;
	public static Object[] noteArray = new Object[2];
	public static int time = 0;
	public static long lastWaitTime = 0;
	
	public static int[][] noteOut = new int[3][]; // notes 'to be played' each bar

	
	public void run()
	{
		while (!Main.stop)
		{
			int waitBar=0;
			if (count==0) {
				waitBar = Main.score.run();
			}
			else {
				waitBar = Main.score.waitBarScore;
			}
			if (lastWaitTime!=0&&(lastWaitTime-System.currentTimeMillis())>0) {
				try {
					Thread.sleep((lastWaitTime-System.currentTimeMillis()),0);
					
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else {
				System.out.println("********NEGATIVE TIME!!! ******** : "+(lastWaitTime-System.currentTimeMillis()));
			}
	
				noteOut = Main.noteOut;
				
			if (Main.noteOut[0]!=null)	{
				ScoreThread scoreThread = new ScoreThread();
				Thread thread = new Thread(scoreThread);
				thread.start();

				
//				waitBar = waitBar - Main.noteOut[2][Main.noteOut[2].length-1]/10;
				int index=0;
				int lastOnset = 0;
				Main.barClk = AltMath.incArray(Main.barClk);
				Main.barClk[Main.barClk.length - 1] = System.currentTimeMillis();
				while (index<noteOut[0].length) {
					try {

						long wait = (long)((noteOut[2][index]-lastOnset)*6000/(Main.bpm*48));
						if (wait>0)
						{
							Thread.sleep(wait,0);
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					noteOut(noteOut[0][index],100,noteOut[2][index]);
					lastOnset = noteOut[2][index];
					index++;
				}
				long wait = (long)((waitBar-lastOnset)*6000/(Main.bpm*48));
				lastWaitTime = wait+System.currentTimeMillis();
			}
			else {
				lastWaitTime = System.currentTimeMillis()+(long)(waitBar*6000/(Main.bpm*48));
			}
			
			count++;
		}
	}
	public void noteOut(int note, int vel,int wait){
		noteArray[0] = note-3;
		if (note<48) {
			note = note%12+49;
		}
		if (note>60) {
			vel = 70;
		}
		else {
			vel = 100;
		}
		noteArray[1] = vel;
//		noteArray[1] = 50;
		System.out.println("------generated note:" + noteArray[0] + " " + noteArray[1] + " " + wait);
		OSC.main(noteArray,"/mididata");
	}
	public void waitSym(double wait) // wait in milliseconds
	{
		long startTime=System.nanoTime();
		long triggerTime=startTime+(long)(1000000*wait*60000/(Main.bpm*48));
		long currentTime=System.nanoTime();
		
		while (currentTime<triggerTime)
		{
			currentTime=System.nanoTime();
		}
	}
	public void waitMil(double wait) // wait in milliseconds
	{
		long startTime=System.nanoTime();
		long triggerTime=startTime+(long)(1000000*wait);
		long currentTime=System.nanoTime();
		
		while (currentTime<triggerTime)
		{
//			triggerTime=startTime+(long)(1000000*wait);
			currentTime=System.nanoTime();
		}
	}
}


class ScoreThread implements Runnable {
	public void run() {
		Main.score.run();
	}
}
